package com.saybiu.service.serviceImpl;

import com.saybiu.dao.*;
import com.saybiu.domain.Comment;
import com.saybiu.domain.Like;
import com.saybiu.domain.Picture;
import com.saybiu.domain.ReplyPost;
import com.saybiu.exception.ServiceException;
import com.saybiu.response.CommonCode;
import com.saybiu.service.ReplyService;
import com.saybiu.utils.Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: 李金祥
 * @Date: 2021/4/18 12:29 （日期和时间）
 */
@Service
public class ReplyServiceImpl implements ReplyService {

    @Resource
    private PostDao postDao;
    @Resource
    private PictureDao pictureDao;
    @Resource
    private ReplyPostDao replyPostDao;
    @Resource
    private CommentDao commentDao;
    @Resource
    private LikeDao likeDao;

    /**
     * 帖子回复量加1 同时给帖子回复表添加记录
     * @param replyPost 帖子回复信息
     * @param pictures 回复帖子是所带的图片
     * 做事务处理
     * 影响bbs_posts、bbs_pictures和bbs_reply_post表
     */
    @Override
    public void replyPost(ReplyPost replyPost, List<Picture> pictures) {

        try {
            postDao.incrCommentNum(replyPost.getReplyPostId());
            replyPostDao.addReplyPost(replyPost);
            pictureDao.addReplyPictures(pictures);
        } catch (Exception e) {
            throw new ServiceException(CommonCode.INSERT_ERROR);
        }
    }

    /**
     * 帖子回复量自减 -1，同时将帖子回复表中的记录和相关图片删掉
     * @param postId 帖子id
     * 做事务处理
     * 影响bbs_posts、bbs_pictures和bbs_reply_post表
     */
    @Override
    public void cancelReplyPost(Integer userId, Integer postId) {
        try {
            postDao.deleteComment(postId);
            pictureDao.deleteReplyPictures(postId);
            replyPostDao.deleteReplyPost(userId, postId);
            commentDao.deleteReplyComment(postId);
        } catch (Exception e) {
            throw new ServiceException(CommonCode.INSERT_ERROR);
        }
    }

    /**
     * 给回复评论表添加记录
     * @param comment 回复评论的内容
     * 操作bbs_reply_comments表
     */
    @Override
    public void addReplyComment(Comment comment) {
        try {
            commentDao.addReplyComment(comment);
        } catch (Exception e) {
            throw new ServiceException(CommonCode.INSERT_ERROR);
        }
    }

    /**
     * 撤销回复评论
     * @param replyId 回复帖子表id
     * 操作bbs_reply_comments表
     */
    @Override
    public void cancelReplyComment(Integer replyId) {
        try {
            commentDao.deleteReplyCommentByReplyId(replyId);
        } catch (Exception e) {
            throw new ServiceException(CommonCode.DELETE_ERROR);
        }
    }

    /**
     * 给帖子评论点赞
     * @param userId 用户id
     * @param likeCommentId 评论id
     */
    @Override
    public void likeReply(Integer userId, Integer likeCommentId) {

        // 查询是否给帖子评论已点赞
        Integer result = likeDao.selectLikeByUserIdAndUserLikeCommentId(userId, likeCommentId);

        if (result == 0) {
            try {
                String replyContent = replyPostDao.selectReplyPostContentByReplyId(likeCommentId);
                Integer postWriterId = replyPostDao.selectReplyPostWriterIdByReplyId(likeCommentId);
                Like like = new Like();
                like.setUserId(userId);
                like.setUserLikeCommentId(likeCommentId);
                like.setUserLikeCommentContent(replyContent);
                like.setUserBelikedId(postWriterId);
                like.setLikeTime(Utils.toDate(new Date()));
                likeDao.addLikePostReply(like);
            } catch (Exception e) {
                throw new ServiceException(CommonCode.INSERT_ERROR);
            }
        } else {
            throw new ServiceException(CommonCode.FAIL);
        }
    }

    /**
     * 取消对帖子评论的点赞
     * @param userId 用户id
     * @param commentId 评论id
     */
    @Override
    public void cancelLikeReply(Integer userId, Integer commentId) {
        try {
            likeDao.deleteLikePostReply(userId, commentId);
        } catch (Exception e) {
            throw new ServiceException(CommonCode.DELETE_ERROR);
        }
    }

    /**
     * 对评论的评论点赞
     * @param userId
     * @param replyCommentId
     */
    public void likeComment(Integer userId, Integer replyCommentId) {
        // 查询是否给评论的评论已点赞
        Integer result = likeDao.selectLikeByUserIdAndReplyCommentId(userId, replyCommentId);

        if (result == 0) {
            try {
                Comment replyComment = commentDao.selectReplyCommentByReplyCommentId(replyCommentId);
                Like like = new Like();
                like.setUserId(userId);
                like.setReplyCommentId(replyCommentId);
                like.setUserLikeCommentContent(replyComment.getReplyCommentContent());
                like.setUserBelikedId(replyComment.getReplyUserId());
                like.setLikeTime(Utils.toDate(new Date()));
                likeDao.addLikeReplyComment(like);
            } catch (Exception e) {
                throw new ServiceException(CommonCode.INSERT_ERROR);
            }
        } else {
            throw new ServiceException(CommonCode.FAIL);
        }


    }

    /**
     * 取消对评论的评论点赞
     * @param userId
     * @param replyCommentId
     */
    public void cancelLikeComment(Integer userId, Integer replyCommentId) {
        try {
            likeDao.deleteLikeReplyComment(userId, replyCommentId);
        } catch (Exception e) {
            throw new ServiceException(CommonCode.DELETE_ERROR);
        }
    }


    /**
     * 根据帖子id查询帖子回复信息
     * @param postId 帖子id
     * @return
     * 操作bbs_reply_post
     */
    @Override
    public List<ReplyPost> queryReplyPostByPostId(Integer postId) {
        return replyPostDao.selectReplyPostByPostId(postId);
    }

    /**
     * 根据回复帖子表的id 查询回复评论表
     * @param replyId
     * @return
     */
    @Override
    public List<Comment> queryReplyComment(Integer replyId) {
        return commentDao.selectReplyCommentByReplyId(replyId);
    }
}
